﻿@model IEnumerable<eBusiness.Models.Product>
@functions {
    private string GetProductLink(int pageNumber = 1)
    {
        var link = "/Admin/Products/Index?";
        if (!string.IsNullOrEmpty(ViewBag.CurrentCategoryID))
        {
            link += "&categoryId=" + ViewBag.CurrentCategoryID;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentName))
        {
            link += "&Name=" + ViewBag.CurrentName;
        }
        if (!string.IsNullOrEmpty((ViewBag.CurrentIsApproved).ToString()))
        {
            link += "&IsApproved=" + ViewBag.CurrentIsApproved;
        }
        if (!string.IsNullOrEmpty((ViewBag.CurrentIsHot).ToString()))
        {
            link += "&IsHot=" + ViewBag.CurrentIsHot;
        }
        if (!string.IsNullOrEmpty((ViewBag.CurrentIsHomepage).ToString()))
        {
            link += "&IsHomepage=" + ViewBag.CurrentIsHomepage;
        }
        if (!string.IsNullOrEmpty((ViewBag.CurrentIsDiscount).ToString()))
        {
            link += "&IsDiscount=" + ViewBag.CurrentIsDiscount;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentStatusID))
        {
            link += "&StatusID=" + ViewBag.CurrentStatusID;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentSoftField))
        {
            link += "&softField=" + ViewBag.CurrentSoftField;
        }
        if (!string.IsNullOrEmpty(ViewBag.CurrentSoftDir))
        {
            link += "&softDir=" + ViewBag.CurrentSoftDir;
        }
        if (!string.IsNullOrEmpty(pageNumber.ToString()))
        {
            link += "&pageNumber=" + pageNumber;
        }
        return link;
    }
}
@{
    ViewBag.Title = "Index";
    Layout = "../Shared/_Layout.cshtml";
    var categoryId = ViewBag.CategoryID;
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#ddlCategory").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });
        $("#txtName").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });

        $("#SoftField").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });
        $("#SoftDir").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });
        $("#ddlStatusId").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });
        $("#cbxIsApproved").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });
        $("#cbxIsHot").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });
        $("#cbxIsHomepage").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });
        $("#cbxIsDiscount").change(function (e) {
            window.location.href = '/Admin/Products?' + GetParams();
        });
    });

    function GetParams() {
        var category = $("#ddlCategory :selected").val();
        var name = $("#txtName").val();
        var softField = $("#SoftField :selected").val();
        var softDir = $("#SoftDir :selected").val();
        var status = $("#ddlStatusId :selected").val();
        var ishot = $("#cbxIsHot").is(":checked");
        //var ishot = $("#cbxIsHot :checked").checked == true ? "true" : "false";
        var ishomepage = $("#cbxIsHomepage").is(":checked");
        var isapproved = $("#cbxIsApproved").is(":checked");
        var isdiscount = $("#cbxIsDiscount").is(":checked");

        var param = '';
        if (!(category == '' || category == null)) {
            param += '&categoryId=' + category;
        }
        if (!(name == '' || name == null)) {
            param += '&Name=' + name;
        }

        if (!(softField == '' || softField == null)) {
            param += '&softField=' + softField;
        }
        if (!(softDir == '' || softDir == null)) {
            param += '&softDir=' + softDir;
        }
        if (!(status == '' || status == null)) {
            param += '&StatusID=' + status;
        }
        if (!(ishot == '' || ishot == null)) {
            param += '&IsHot=' + ishot;
        }
        if (!(ishomepage == '' || ishomepage == null)) {
            param += '&IsHomepage=' + ishomepage;
        }
        if (!(isapproved == '' || isapproved == null)) {
            param += '&IsApproved=' + isapproved;
        }
        if (!(isdiscount == '' || isdiscount == null)) {
            param += '&IsDiscount=' + isdiscount;
        }
        return param;
    }
</script>
<h2>
    Products</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div style="float: right">
    @Html.ActionLink("Export to excel", "Export", new { categoryId = ViewBag.CurrentCategoryID, IsHot = ViewBag.CurrentIsHot, IsApproved = ViewBag.CurrentIsApproved, IsHomepage = ViewBag.CurrentIsHomepage, IsDiscount = ViewBag.CurrentIsDiscount })
</div>
<div class="clearboth">
    <span></span>
</div>
<div class="adminfilterdiv">
    <ul class="productFilter">
        <li><span>Name : </span><span>
            @Html.TextBox("txtName", (string)@ViewBag.CurrentName, new {@style = "width:150px"})
        </span></li>
        <li><span>Category : </span><span>
            @Html.DropDownList("ddlCategory", (SelectList)ViewBag.Categories, string.Empty)
        </span></li>
        <li><span>Status : </span><span>
            @Html.DropDownList("ddlStatusId", (SelectList)ViewBag.StatusId, string.Empty)
        </span></li>
        @*<li>
            <span>Configuration : </span>
            <span>
                @Html.DropDownList("Configuration", (SelectList)ViewBag.Configuration)
            </span>
        </li>*@
        <li><span>Sort By : </span><span>
            @Html.DropDownList("SoftField", (SelectList)ViewBag.SoftFields, string.Empty)
        </span></li>
        <li><span>Sort Dir : </span><span>
            @Html.DropDownList("SoftDir", (SelectList)ViewBag.SoftDir, string.Empty)
        </span></li>
    </ul>
</div>
<div class="adminLeftFilterDiv">
    <ul class="productLeftFilter">
        <li><span>Is approved : </span><span>@*  <input type="checkbox" id="cbxIsApproved" value="@ViewBag.CurrentIsApproved" />*@
            @Html.CheckBox("cbxIsApproved", (bool)@ViewBag.CurrentIsApproved)
        </span></li>
        <li><span>Is homepage : </span><span>@*<input type="checkbox" id="cbxIsHomepage" value="@ViewBag.CurrentIsHomepage" />*@
            @Html.CheckBox("cbxIsHomepage", (bool)@ViewBag.CurrentIsHomepage)
        </span></li>
        <li><span>Is hot : </span><span>@*<input type="checkbox" id="cbxIsHot" value="@ViewBag.CurrentIsHot" />*@
            @Html.CheckBox("cbxIsHot", (bool)@ViewBag.CurrentIsHot)
        </span></li>
        <li><span>Is discount : </span><span>
            @Html.CheckBox("cbxIsDiscount", (bool)@ViewBag.CurrentIsDiscount)
        </span></li>
    </ul>
</div>
<table class="dashboardTable">
    <tr>
        <th style="width: 95px;">
            ThumbImage
        </th>
        <th style="width: 250px">
            Product
        </th>
        <th style="width: 250px">
            Product Details
        </th>
        <th style="width: 200px">
            Price
        </th>
        <th>
            Configuration
        </th>
        <th>
            Action
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                <img src="@item.ThumbImage" alt="image" class="thumbImage" />
            </td>
            <td>
                <table class="detailsTables">
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Code
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Code)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Name
                        </td>
                        <td>
                            <a href="@Url.Action("Details", "Products", new { id = item.ID })">@Html.DisplayFor(modelItem => item.Name)</a>
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Categories
                        </td>
                        <td>
                            @foreach (var catItem in item.Categories)
                            {
                                <a href="@Url.Action("Details", "Categories", new { id = catItem.ID })">@Html.DisplayFor(modelItem => catItem.Name)</a>    
                                @:,
                                                                                                                }
                        </td>
                    </tr>
                </table>
            </td>
            <td>
                <table class="detailsTables">
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Color
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Color)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Size
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Size)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Type
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Type)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Made From
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.MadeFrom)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Made by
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.MadeBy)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Manufacture
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Manufacture.Name)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Warranty
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Warranty)
                        </td>
                    </tr>
                </table>
            </td>
            <td>
                <table class="detailsTables">
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Price
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Price)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Original Price
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.OriginalPrice)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Status
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Status.Name)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Quatity
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.QuantityInStock)
                        </td>
                    </tr>
                </table>
            </td>
            <td>
                <table class="detailsTables">
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Is Approved
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.IsApproved)
                        </td>
                    </tr>
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Is homepage
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.IsHomepage)
                        </td>
                    </tr>
                    @if (@item.IsHomepage && (!string.IsNullOrEmpty(item.IsHomepageEndDate.ToString()) || !string.IsNullOrEmpty(item.IsHomepageStartDate.ToString())))
                    {
                        <tr>
                            <td colspan="2">
                                <span class="leftDetailsTableColumn">From </span>
                                @if (string.IsNullOrEmpty(item.IsHomepageStartDate.ToString()))
                                {
                                    <span>n/a</span>
                                }
                                else
                                {
                                    @item.IsHomepageStartDate.Value.ToString("dd/MM/yyyy");
                                }
                                <span class="leftDetailsTableColumn">To </span>
                                @if (string.IsNullOrEmpty(item.IsHomepageEndDate.ToString()))
                                {
                                    <span>n/a</span>
                                }
                                else
                                {
                                    @item.IsHomepageEndDate.Value.ToString("dd/MM/yyyy");
                                }
                            </td>
                        </tr>
                    }
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Is hot
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.IsHot)
                        </td>
                    </tr>
                    @if (@item.IsHot && (!string.IsNullOrEmpty(item.IsHotStartDate.ToString()) || !string.IsNullOrEmpty(item.IsHotEndDate.ToString())))
                    {
                        <tr>
                            <td colspan="2">
                                <span class="leftDetailsTableColumn">From </span>
                                @if (string.IsNullOrEmpty(item.IsHotStartDate.ToString()))
                                {
                                    <span>n/a </span>
                                }
                                else
                                {
@*@Html.DisplayFor(modelItem => item.IsHotStartDate.Value.ToString("dd/MM/yyyy"))*@
                                    @item.IsHotStartDate.Value.ToString("dd/MM/yyyy")
                                }
                                <span class="leftDetailsTableColumn">To </span>
                                @if (string.IsNullOrEmpty(item.IsHotEndDate.ToString()))
                                {
                                    <span>n/a </span>
                                }
                                else
                                {
@*@Html.DisplayFor(modelItem => item.IsHotEndDate.Value.ToString("dd/MM/yyyy"))*@
                                    @item.IsHotEndDate.Value.ToString("dd/MM/yyyy")
                                }
                            </td>
                        </tr>
                    }
                    <tr>
                        <td class="leftDetailsTableColumn">
                            Is discount
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.IsDiscount)
                        </td>
                    </tr>
                    @if (@item.IsDiscount && (!string.IsNullOrEmpty(item.DiscountStartDate.ToString()) || !string.IsNullOrEmpty(item.DiscountEndDate.ToString())))
                    {
                        <tr>
                            <td colspan="2">
                                <span class="leftDetailsTableColumn">From </span>
                                @if (string.IsNullOrEmpty(item.DiscountStartDate.ToString()))
                                {
                                    <span>n/a </span>
                                }
                                else
                                {
@*@Html.DisplayFor(modelItem => item.DiscountStartDate)*@
                                    @item.DiscountStartDate.Value.ToString("dd/MM/yyyy")
                                }
                                <span class="leftDetailsTableColumn">To </span>
                                @if (string.IsNullOrEmpty(item.DiscountEndDate.ToString()))
                                {
                                    <span>n/a</span>
                                }
                                else
                                {
@*@Html.DisplayFor(modelItem => item.DiscountEndDate)*@
                                    @item.DiscountEndDate.Value.ToString("dd/MM/yyyy");
                                }
                            </td>
                        </tr>
                    }
                </table>
            </td>
            <td>
                <ul class="ulTableAction">
                    <li>@Html.ActionLink("Edit", "Edit", new { id = item.ID })</li>
                    <li>@Html.ActionLink("Details", "Details", new { id = item.ID })</li>
                    <li>@Html.ActionLink("Delete", "Delete", new { id = item.ID })</li>
                    @if (!item.IsApproved)
                    {
                        <li>@Html.ActionLink("Approve", "Approved", new { id = item.ID })</li>
                    }
                    else
                    {
                        <li>@Html.ActionLink("Un-Approve", "Approved", new { id = item.ID })</li>
                    }
                </ul>
            </td>
        </tr>
    }
</table>
<div class="common_paging">
    <ul class="nav_common_ul">
        @{
            if (ViewBag.CurrentPage > 1)
            {
                
            <li><a href="@GetProductLink()" class="common_paging_first">First</a></li>
            <li><a href="@GetProductLink((int)ViewBag.CurrentPage - 1)" class="common_paging_previous">
                Prev</a></li>
            }
            <li>page
                <input id="goToPage" type="text" class="common_pagging_page" style="width: 50px" value="@ViewBag.CurrentPage"/>
                of @ViewBag.NumberOfPage
            </li>
            if (ViewBag.CurrentPage < ViewBag.NumberOfPage)
            {
            <li><a href="@GetProductLink((int)ViewBag.CurrentPage + 1)" class="common_paging_next">
                Next</a></li>
            <li><a href="@GetProductLink((int)ViewBag.NumberOfPage)" class="common_paging_last">
                Last</a></li>
            }
        }
    </ul>
</div>
